﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Gmantis.Controls
{
    [TemplateVisualState(Name = "Determinate", GroupName = "CommonStates"), TemplateVisualState(Name = "Indeterminate", GroupName = "CommonStates")]
    public class ProgressBar : RangeBase
    {
        // Fields
        internal bool _isLoaded;
        private ProgressBarTemplateSettings _templateSettings;
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ProgressBar), null);
        public static readonly DependencyProperty IndeterminateForegroundProperty = DependencyProperty.Register("IndeterminateForeground", typeof(Brush), typeof(ProgressBar), null);
        public static readonly DependencyProperty IsIndeterminateProperty = DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(ProgressBar), new PropertyMetadata(new PropertyChangedCallback(ProgressBar.OnIsIndeterminatePropertyChanged)));

        // Methods
        public ProgressBar()
        {
            RoutedEventHandler handler = null;
            DependencyPropertyChangedEventHandler handler2 = null;
            base.DefaultStyleKey = typeof(ProgressBar);
            if (handler == null)
            {
                handler = (param0, param1) => this.ChangeVisualStateCommon(false);
            }
            base.Loaded += handler;
            if (handler2 == null)
            {
                handler2 = (s, a) => this.ChangeVisualStateCommon(true);
            }
            base.IsEnabledChanged += handler2;
            this.SetCustomDefaultValues();
        }

        protected void ChangeVisualStateCommon(bool useTransitions)
        {
            if (!this.IsIndeterminate)
            {
                VisualStateHelper.GoToState(this, "Determinate", useTransitions);
            }
            if (this.IsIndeterminate)
            {
                VisualStateHelper.GoToState(this, "Indeterminate", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to ProgressBar.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.ChangeVisualStateCommon(false);
            }
        }

        private static void OnIsIndeterminatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ProgressBar).ChangeVisualStateCommon(true);
        }

        private void SetCustomDefaultValues()
        {
            this._templateSettings = new ProgressBarTemplateSettings(this);
        }

        // Properties
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public Brush IndeterminateForeground
        {
            get { return (Brush)GetValue(IndeterminateForegroundProperty); }
            set { SetValue(IndeterminateForegroundProperty, value); }
        }

        public bool IsIndeterminate
        {
            get { return (bool)GetValue(IsIndeterminateProperty); }
            set { SetValue(IsIndeterminateProperty, value); }
        }

        public ProgressBarTemplateSettings TemplateSettings
        {
            get { return this._templateSettings; }
        }
    }
}
